package com.shd.edanpei.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.shd.edanpei.R;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.base.data.RxBusCode;
import com.shd.edanpei.base.rxbus.RxBus;
import com.shd.edanpei.base.rxbus.Subscribe;
import com.shd.edanpei.bean.PicBean;
import com.shd.edanpei.presenter.contract.PerfectDataContract;
import com.shd.edanpei.db.DaoManager;
import com.shd.edanpei.enums.PicEnum;
import com.shd.edanpei.presenter.PerfectDataPresenter;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by mc on 2018/9/18.
 * 完善资料
 */
public class PerfectDataActivity extends BaseActivity<PerfectDataPresenter> implements PerfectDataContract.PerfectDataIView {

    @BindView(R.id.headline)
    TextView headline;
    @BindView(R.id.ll_root1)
    View root1;
    @BindView(R.id.et_companyName)
    EditText etCompanyName;
    @BindView(R.id.tv_license)
    TextView tvLicense;
    @BindView(R.id.tv_companySize)
    TextView tvCompanySize;
    @BindView(R.id.tv_workArea)
    TextView tvWorkArea;
    @BindView(R.id.et_ban_name)
    EditText etBankName;
    @BindView(R.id.et_branch)
    EditText etBranch;
//    @BindView(R.id.et_workNo)
//    EditText etWorkNo;
//    @BindView(R.id.tv_bankArea)
//    TextView tvBankArea;
    @BindView(R.id.tv_bankAddress)
    TextView tvBankAddress;


    @BindView(R.id.et_detailedAddress)
    EditText etDetailedAddress;
    @BindView(R.id.tv_submit)
    TextView tvSubmit;

    @BindView(R.id.ll_root2)
    View root2;

    private int roleType;

    @Override
    protected int setLayout() {
        return R.layout.activity_perfectdata;

    }

    @Override
    protected PerfectDataPresenter bindPresenter() {
        return new PerfectDataPresenter(this);
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        RxBus.get().register(this);
        headline.setText(getResources().getString(R.string.perfect_data));

        roleType = DaoManager.getInstance().getUserBean().getRoleType();
        if (roleType == 2) {
            //银行经理
            root2.setVisibility(View.VISIBLE);
        } else {
            //房、车老板
            root1.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void initListener() {
        etCompanyName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence s, int i, int i1, int i2) {
                mPresenter.setCompanyName(s.toString(), roleType);
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        etBankName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mPresenter.setCompanyName(s.toString(), roleType);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        etBranch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mPresenter.setBranch(s.toString(), roleType);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
//        etBelongStation.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int i, int i1, int i2) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int i, int i1, int i2) {
//                mPresenter.setBelongStation(s.toString());
//            }
//
//            @Override
//            public void afterTextChanged(Editable editable) {
//
//            }
//        });
//        etWorkNo.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//                mPresenter.setWorkNo(s.toString(), roleType);
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//
//            }
//        });
        etDetailedAddress.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence s, int i, int i1, int i2) {
                mPresenter.setDetailedAddress(s.toString(), roleType);
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        tvSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPresenter.submit(roleType);
            }
        });
    }

    @OnClick(R.id.iv_headline_back)
    void close() {
        startActivity(LoginActivity.class);
        DaoManager.getInstance().logout();
        finish();
    }

    PicBean picBean = new PicBean();

    //营业执照
    @OnClick(R.id.ll_license)
    void onLicense() {
        Bundle bundle = new Bundle();
        bundle.putInt(Constant.TYPE, PicEnum.businessCert.getId());
        bundle.putParcelable(Constant.DATA, picBean);
        startActivity(UpFileActivity.class, bundle);
    }

    //公司规模
    @OnClick(R.id.ll_companySize)
    void onCompanySize() {
        mPresenter.onCompanySize(tvCompanySize, roleType);
    }

    //办公区域
    @OnClick(R.id.ll_workArea)
    void onWorkArea() {
        mPresenter.onWorkArea(tvWorkArea, roleType);
    }

//
//    @OnClick(R.id.ll_bankArea)
//    void onBankArea() {
//        mPresenter.onWorkArea(tvBankArea, roleType);
//    }

    @OnClick(R.id.ll_bankAddress)
    void onBankAddress() {
        mPresenter.setBankAddress(tvBankAddress, roleType);
    }
//
//    @OnClick(R.id.ll_enterArea)
//    void onEnterArea() {
//        mPresenter.setEnterArea(tvEnterArea);
//    }
//
//    @OnClick(R.id.ll_companyAddress)
//    void onCompanyAddress() {
//        mPresenter.setCompanyAddress(tvCompanyAddress);
//    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            startActivity(LoginActivity.class);
            DaoManager.getInstance().logout();
            finish();
        }
        return false;
    }

    @Override
    public TextView getSubmitView() {
        return tvSubmit;
    }

    @Override
    public void setText(TextView text, String str) {
        text.setText(str);
        text.setTextColor(getResources().getColor(R.color.black));
    }


    @Subscribe(code = RxBusCode.FILE_UPLOAD_SUCCESS)
    public void setFileData(PicBean bean) {
        int type = bean.getType();
//        if (type == PicEnum.idCardPic.getId()) {
//            picBean.setIdCardPic(bean.getIdCardPic());
//        } else if (type == PicEnum.householdRegister.getId()) {
//            picBean.setHouseholdRegister(bean.getHouseholdRegister());
//        } else if (type == PicEnum.roomCert.getId()) {
//            picBean.setRoomCert(bean.getRoomCert());
//        } else if (type == PicEnum.businessCert.getId()) {
//            picBean.setBusinessCert(bean.getBusinessCert());
//        } else if (type == PicEnum.otherHouseCert.getId()) {
//            picBean.setOtherHouseCert(bean.getOtherHouseCert());
//        } else if (type == PicEnum.carCert.getId()) {
//            picBean.setCarCert(bean.getCarCert());
//        } else if (type == PicEnum.carWarranty.getId()) {
//            picBean.setCarWarranty(bean.getCarWarranty());
//        } else if (type == PicEnum.maritalCert.getId()) {
//            picBean.setMaritalCert(bean.getMaritalCert());
//        } else if (type == PicEnum.supplement.getId()) {
//            picBean.setSupplement(bean.getSupplement());
//        }
        picBean.setBusinessCert(bean.getBusinessCert());
        setText(tvLicense, "已上传");
        mPresenter.setBusinessLicense(bean.getBusinessCert(), roleType);
    }


    @Override
    protected void onDestroy() {
        RxBus.get().unRegister(this);
        super.onDestroy();
    }
}
